import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class Stutter {
	public static void main(String[] args) {
		//Check the number of input arguments(file names)
		if(args.length==1)
			findStutter(args[0]); 
		else
			System.out.println("Error: Stutter can't open many files!");
	}
	
	//Check for Repeated words upon passing the file name as runtime argument.
	static void findStutter(String fileName){
		String word1 = null;
		String word2 = null;
		try {
			BufferedReader bf = new BufferedReader(new FileReader(fileName));
			String line = null;
			String[] tokens = null;
			int lineCount=0;
			try {
				while((line = bf.readLine())!= null){
					lineCount++;
					tokens= splitBasedOnDelimiter(line);
		
					for(int i=0; i<tokens.length; i++){
						word1 = tokens[i];
	 						if(word1.equals(word2))
								System.out.println("Repeated word on line "+ lineCount+ " : "+ word1 + " " + word2);
  							else
								word2 = word1;
 					}
					word2 = tokens[tokens.length-1];
				} 	
			} catch (IOException e) {
			 e.printStackTrace();
			 System.out.println("Exception: IO Exception caught!");
			}
							
		} catch (FileNotFoundException e) {
			 e.printStackTrace();
			 System.out.println("Exception: File Not Found!");
		}		
	}
	

//Check the Delimiters in each line	
	static String[] splitBasedOnDelimiter(String line){
		String[] tokens = null;
		String delimiter = "[\\n ,.\\\\!-+=;:?&{}]+";
		
		tokens = line.split(delimiter);
		return tokens;
  	}	
}
